package com.kinon.bs_app.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.kinon.bs_app.R;
import com.kinon.bs_app.beans.MsgDataBean;

import java.util.List;

public class MsgAdapter extends RecyclerView.Adapter<MsgAdapter.MyHolder> {

    private Context context;
    private List<MsgDataBean> list;

    public MsgAdapter(Context context, List<MsgDataBean> list) {
        this.context = context;
        this.list = list;
    }

    public void update(List<MsgDataBean> list) {
        this.list = list;
        notifyDataSetChanged();
    }

    @Override
    public MyHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
        View view = LayoutInflater.from(viewGroup.getContext())
                .inflate(R.layout.item_msg_layout, viewGroup, false);
        MyHolder myHolder = new MyHolder(view);
        return myHolder;
    }

    @Override
    public void onBindViewHolder(MyHolder myHolder, int i) {
        String exchange = list.get(i).getEXCHANGE();
        String coin = list.get(i).getCOIN();
        String add_time = list.get(i).getAdd_time();
        String myTime = add_time.substring(10) +" "
                + add_time.substring(5, 10);
        final String msg = list.get(i).getMsg();

        myHolder.tv_exchange.setText(exchange);
        myHolder.tv_coin.setText(coin);
        myHolder.tv_time.setText(myTime);
        myHolder.tv_msg.setText(msg);

        myHolder.ll_msg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(context, msg,
                        Toast.LENGTH_LONG).show();
            }
        });
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    public static class MyHolder extends RecyclerView.ViewHolder {
        public LinearLayout ll_msg;
        public TextView tv_exchange;
        public TextView tv_coin;
        public TextView tv_time;
        public TextView tv_msg;

        public MyHolder(View v) {
            super(v);
            ll_msg = v.findViewById(R.id.ll_msg);
            tv_exchange = v.findViewById(R.id.tv_exchange);
            tv_coin = v.findViewById(R.id.tv_coin);
            tv_time = v.findViewById(R.id.tv_time);
            tv_msg = v.findViewById(R.id.tv_msg);
        }
    }
}
